package Dao.Process.Imp;

import Dao.Process.TaskDao;
import Entity.Process.Task;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TaskDaoImp implements TaskDao {
    private PreparedStatement ps = null;
    private ResultSet rs = null;


    @Override
    public Task getTaskBySNO(String SNO, Connection conn) {
        Task task = null;
        String sql = "select task_id, title, collname, mname, tno, sno, state ,opinion,is_pass from task where sno = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, SNO);
            rs = ps.executeQuery();
            if (rs.next()) {
                Integer taskId = rs.getInt("task_id");
                String title = rs.getString("title");
                String collname = rs.getString("collname");
                String mname = rs.getString("mname");
                String tno = rs.getString("tno");
                String sno = rs.getString("sno");
                String state = rs.getString("state");
                String opinion = rs.getString("opinion");
                String isPass = rs.getString("is_pass");
                task = new Task(taskId, title, collname, mname, tno, sno, state, opinion, isPass);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return task;
    }

    @Override
    public Integer insertTask(Task task, Connection conn) {
        int result = 0;
        try {
            String sql = "insert into task(title, collname, mname, tno, sno, state , opinion, is_pass) " +
                    "values (?,?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, task.getTitle());
            ps.setString(2, task.getCollname());
            ps.setString(3, task.getMname());
            ps.setString(4, task.getTno());
            ps.setString(5, task.getSno());
            ps.setString(6, task.getState());
            ps.setString(7, task.getOpinion());
            ps.setString(8, task.getIsPass());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }

    @Override
    public Integer updateCheck(Integer id, String result, String opinion,String state, Connection conn) {
        int res = 0;
        try {
            String sql = "update task set state = ? , opinion = ? , is_pass = ? where task_id = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1,state);
            ps.setString(2, opinion);
            ps.setString(3, result);
            ps.setInt(4, id);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }

    @Override
    public Integer getSumByTNO(String tno, Integer type, Connection conn) {
        int res = 0;
        try {
            String sql;
            if (type == 1) {
                sql = "select COUNT(sno) from task where tno = ? ";
            } else {
                sql = "select COUNT(sno) from task where tno = ? and is_pass = '通过' ";
            }
            ps = conn.prepareStatement(sql);
            ps.setString(1, tno);
            rs = ps.executeQuery();
            if (rs.next()) {
                res = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }
}
